const filterMenu = (menus) => {
  const role = localStorage.getItem("role");
  const authMenus = [];
  const _menus = JSON.parse(JSON.stringify(menus));
  _menus.forEach((menu) => {
    if (menu.roles === "*" || menu.roles.includes(role)) {
      if (menu.children && menu.children.length > 0) {
        menu.children = filterMenu(menu.children);
      }
      authMenus.push(menu);
    }
  });
  return authMenus;
};
export default {
  namespaced: true,
  state: {
    // 导航数组
    menus: [
      {
        label: "仪表盘",
        icon: "Compass",
        path: "/dashboard",
        roles: "*",
      },
      {
        label: "商品管理",
        icon: "Goods",
        roles: ["admin", "a", "b"],
        children: [
          {
            label: "商品列表",
            roles: "*",
            path: "/itemLists",
          },
          {
            label: "商品分类列表",
            roles: ["admin", "a", "b"],
            path: "/cateLists",
          },
        ],
      },
      {
        label: "订单",
        icon: "DocumentAdd",
        path: "/orderLists",
        roles: "*",
      },
      {
        label: "设置",
        icon: "Setting",
        path: "/setting",
        roles: ["a", "b"],
      },
    ],
  },
  getters: {
    // 过滤当前角色可以访问的 导航菜单
    authMenus: (state) => {
      return filterMenu(state.menus);
    },
  },
};
